package com.hiwan.unions.activity.main.fragment;

import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.main.fragment.base.RootFragment;
import com.hiwan.unions.adapter.bean.EventFragmentBean;
import com.hiwan.unions.adapter.fragmentAdapters.EventAdapter;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.view.NavigationView;
import com.hiwan.unions.view.PullToRefreshLayout;
import com.hiwan.unions.view.materialspinner.MaterialSpinner;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.ViewStatus;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by llg on 2017/6/7.
 */
public class EventFragment extends RootFragment implements HttpListener<JSONObject> {
    private MaterialSpinner spinner1, spinner2, spinner3;
    private ListView myListview;
    private EventAdapter eventAdapter;
    private List<EventFragmentBean> cDates = new ArrayList<EventFragmentBean>();
    public static final int[] RES = new int[]{R.mipmap.zg_event, R.mipmap.sh_event, R.mipmap.ymq_event, R.mipmap.js_event, R.mipmap.wd_event};
    public String[] title, date, number;
    private NavigationView navigationView;
    private PullToRefreshLayout refreshLayouts;
    private TextView empty_text;
    private JSONObject jsonObject;
    private int pageNumber = 1, pageSize = 10;

    @Override
    public int getLayout() {
        canLoadMore = false;
        canRefresh = false;
        return R.layout.fragment_event;
    }

    @Override
    protected void Interface() {
        super.Interface();
        // 当SDK版本号为19以上版本时
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            findView(R.id.RelaButtonTitlenear).setVisibility(View.VISIBLE);
        }
        navigationView = findView(R.id.navigationView);
        navigationView.getGoBackBtn().setVisibility(View.INVISIBLE);
        title = getResources().getStringArray(R.array.title);
        date = getResources().getStringArray(R.array.date);
        number = getResources().getStringArray(R.array.number);
        for (int i = 0; i < RES.length; i++) {
            EventFragmentBean info = new EventFragmentBean();
            info.setImage(RES[i]);
            info.setNumber(number[i]);
            info.setDate(date[i]);
            info.setTitle(title[i]);
            cDates.add(info);
        }
        refreshLayouts = getView(R.id.refreshLayouts);
        refreshLayouts.setCanLoadMore(true);
        refreshLayouts.setCancanRefresh(true);
        refreshLayouts.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 结束刷新
                        refreshLayouts.finishRefresh();
                    }
                }, 2000);
            }

            @Override
            public void loadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 结束加载更多
                        refreshLayouts.finishLoadMore();
                    }
                }, 2000);
            }
        });

        eventAdapter = new EventAdapter(getActivity(), cDates);
        myListview = findView(R.id.myListview);
        myListview.setAdapter(eventAdapter);

        spinner1 = findView(R.id.spinner1);
        spinner1.setItems("全省", "成都市", "宜宾市");
        spinner1.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                Logger.i("position   " + position);
                initData();
            }
        });
        spinner2 = findView(R.id.spinner2);
        spinner2.setItems("全部分类", "旅游休闲", "教育培训", "医疗健康", "文化体育", "生活服务", "其他");
        spinner2.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                initData();
            }
        });
        spinner3 = findView(R.id.spinner3);
        spinner3.setItems("进行中", "已结束");
        spinner3.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                initData();
            }
        });
    }

    @Override
    protected void initData() {
        showLoading();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // 结束刷新
                showeMPTY("暂无数据");
            }
        }, 2000);
    }

    /**
     * 显示加载中状态
     */
    protected void showLoading() {
        refreshLayouts.showView(ViewStatus.LOADING_STATUS);

    }

    /**
     * 加载完成的状态
     */
    protected void showContentView() {
        refreshLayouts.showView(ViewStatus.CONTENT_STATUS);
    }

    /**
     * 加载失败点击重新加载的状态
     */
    protected void showeMPTY(String text) {
        Logger.i("执行？？" + text);
        refreshLayouts.showView(ViewStatus.EMPTY_STATUS);
        View error = refreshLayouts.getView(ViewStatus.EMPTY_STATUS);
        empty_text = findView(error, R.id.empty_text);
        empty_text.setText(text);

    }

    /**
     * 网络显示错误
     */
    protected void showError() {
        finishRefresh();
        finishLoadMore(false);
        refreshLayouts.showView(ViewStatus.ERROR_STATUS);
        View error = refreshLayout.getView(ViewStatus.ERROR_STATUS);
        findView(error, R.id.shuax).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }


    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        jsonObject = response.get();
        JSONArray jsonArray = JsonUtil.getJSONArray(jsonObject, "date");
        if (pageNumber == 1) {
            cDates.clear();
            eventAdapter.notifyDataSetChanged();
        }
        finishRefresh();
        finishLoadMore(JsonUtil.getBoolean(jsonObject, "lastPage"));
        if (jsonArray.length() == 0 && pageNumber == 1) {
            showeMPTY("您还没有相关消息");
        } else {
            showContentView();
        }
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {
        if (type) {
            showError();
        }
    }
}
